//
//  SalesExecutiveVC.m
//  WestAir
//
//  Created by s on 16/9/25.
//  Copyright © 2016年 Henry. All rights reserved.
//

#import "SalesExecutiveVC.h"
#import "SaleExcueContent.h"
#import "BackGroundImageView.h"
#import "AddMember.h"

@interface SalesExecutiveVC ()<saleExcueTableCellDelegate>

@property(nonatomic,strong)NSMutableArray  *arrMember;

@end

@implementation SalesExecutiveVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    HangBanInfo   *hangBan=[HangBanInfo  currentHangBan];
    _arrMember=[[NSMutableArray  alloc]  initWithArray:hangBan.arrMember copyItems:YES];
    
    
    
}

#pragma mark Tableview Deleget

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return  _arrMember.count?_arrMember.count+3:0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    UITableViewCell  *cell=[tableView dequeueReusableCellWithIdentifier:@"SaleExcueTitle"];
    
    
    if (indexPath.row>0&&indexPath.row<=_arrMember.count) {
        cell=[tableView dequeueReusableCellWithIdentifier:@"SaleExcueContent"];
        SaleExcueContent  *cellExcueContent=(SaleExcueContent  *)cell;
        cellExcueContent.btnDelate.tag=indexPath.row-1;
        MemberList  *member=_arrMember[indexPath.row-1];
        
        cellExcueContent.lableName.text=member.userName;
        cellExcueContent.lableContent.text=member.post;
         
        cellExcueContent.isExcuPeo.text= [member.isSaleHost  isEqualToString:@"1"]?@"是":@"";
        
        cellExcueContent.delegate=self;

    }
    else  if(indexPath.row==_arrMember.count+1){
    
        cell=[tableView dequeueReusableCellWithIdentifier:@"AddPeo"];
        BackGroundImageView  *backGroundView=[cell.contentView viewWithTag:100];
        if (indexPath.row==_arrMember.count) {
            backGroundView.type=cornerBottomLeftAndRight;
        }
        else{
            backGroundView.type=cornerDefault;
            
        }
    
    }
    else  if(indexPath.row==_arrMember.count+2){
    
      cell=[tableView dequeueReusableCellWithIdentifier:@"SaleExcueBottom"];
     
    }
    else if(indexPath.row==0){
        BackGroundImageView *imageBackGround=[cell.contentView viewWithTag:100];
        imageBackGround.type=cornerTopLeftAndRight;

    }
    cell.selectionStyle =UITableViewCellSelectionStyleNone;
    
    return cell;
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    

    
    if (indexPath.row==0||indexPath.row==_arrMember.count+2) {
        return;
    }
    else if (indexPath.row==_arrMember.count+1){
    
       [self performSegueWithIdentifier:@"addMember" sender:@"addMember" ];
    }
    else{
    
        
        MemberList  *memberSelect=_arrMember[indexPath.row-1];
        
        [_arrMember  enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            
            MemberList  *member=obj;
            if (memberSelect==member) {
                member.isSaleHost=@"1";
            }
            else{
                
                member.isSaleHost=@"0";
            }
            
            ;
        }];
        
        [tableView reloadData];

    }
    
    
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.row==_arrMember.count+2) {
        return   84   ;
    }
    else if (indexPath.row==0){
        return 49;
    }
    else{
        
        return 35;
        
    };
    
}
- (IBAction)popBack:(id)sender {
    
    [self.navigationController  popViewControllerAnimated:YES];
}
- (IBAction)subMit:(id)sender {
    
     __block  BOOL  isSale=NO;
    [_arrMember  enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        MemberList  *member=obj;
        if ( [member.isSaleHost isEqualToString:@"1"]) {
            isSale=YES;
        }
        ;
    }];
    if (isSale==NO) {
        [CMCCIOTTipsCenter showErrorWithStatus:@"请选择销售主持人"];
        return;
    }
    
    
    HangBanInfo  *hangBan=[HangBanInfo currentHangBan];
    NSInteger  FID=hangBan.FID;
    
    NSMutableArray  *arrList=[[NSMutableArray  alloc]  init];
    
    [_arrMember enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        NSMutableDictionary  *dicTem=[[NSMutableDictionary  alloc]  init];
        MemberList  *member=obj;
        
        [dicTem  setObject:[NSNumber numberWithInteger:member.FEntryID] forKey:@"FEntryID"];
        [dicTem  setObject:member.isSaleHost forKey:@"isSaleHost"];
        [dicTem   setObject:[NSNumber numberWithInteger:member.userID] forKey:@"userID"];
        [arrList addObject:dicTem];
        
        
        
        
    }];
    
    NSDictionary  *dicTem=@{@"FID":[NSNumber numberWithInteger:FID],@"list":arrList};
    
    NSDictionary  *dicJson= [CMCCIOTIGUnity   getRequestJson:dicTem];
    [[NetworkFactory  alloc]  startPostRequestWithURL:SETSALHOST andPostValues:dicJson andSuccessBlock:^(NSDictionary *responseJson) {
        
        if ([responseJson[@"IsSuccess"]  integerValue]==1) {
            
            HangBanInfo  *currentHangBan=[HangBanInfo currentHangBan];
            currentHangBan.FSubmitSaleHost=@"1";

            [CMCCIOTTipsCenter  showSuccessWithStatus:responseJson[@"message"]];
            [self.navigationController  popViewControllerAnimated:YES];
            
            hangBan.arrMember=_arrMember;
        }
        
        
    } andFailBlock:^(NSError *error) {
        
        
    } ];
    
    
    
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(nullable id)sender NS_AVAILABLE_IOS(5_0){
    
    
    if([sender  isEqualToString:@"addMember"]){
        
        AddMember  *addInvertory=segue.destinationViewController;
        addInvertory.vertoryBlock=^(id data){
        
        MemberList  *memberSelect=(MemberList *)data;
            
        __block  BOOL  isHas=NO;
        [_arrMember enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            MemberList  *member=obj;
            if(member.userID==memberSelect.userID){
                
                isHas=YES;
   
            }
           
            ;
        }];
            
        if(isHas==YES){
            
            [CMCCIOTTipsCenter  showErrorWithStatus:@"乘务人员已经存在！"];
            return ;
            
        }
        else{
                memberSelect.FEntryID=0;
                [_arrMember  addObject:memberSelect];
            }
            [self.mainTableView  reloadData ];
        
        };
        
        
    }
    else{
        

    }
    
}

-(void)delatePeo:(NSInteger)tag{

    [_arrMember removeObjectAtIndex:tag];
    [self.mainTableView reloadData];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
